import { _decorator, Component, Node, input, EventTouch, Input, Prefab, instantiate, Collider, BoxCollider, ITriggerEvent, AudioSource } from 'cc';
import { Bullet } from './bullet/bullet';
import { constant } from './constant';
import { AudioManager } from './framework/AudioManager';
import { PoolManager } from './framework/PoolManager';
const { ccclass, property } = _decorator;

@ccclass('planeController')
export class planeController extends Component {
    @property
    public maxHP = 0;
    @property(Prefab)
    public bullet01: Prefab = null;
    @property(Prefab)
    public bullet02: Prefab = null;
    @property(Prefab)
    public bullet03: Prefab = null;
    @property(Prefab)
    public bullet04: Prefab = null;
    @property(Prefab)
    public bullet05: Prefab = null;
    @property(AudioManager)
    public audioManager: AudioManager = null;
    @property
    bulletSpeed = 1;
    @property
    shootTime = 0.3

    @property(Node)
    explode: Node = null;
    @property(Node)
    bulletRoot: Node = null;
    @property(Node)
    Hp: Node = null;

    public curHP = this.maxHP;
    public _isShooting: boolean = false;
    public _isDead: boolean = false;

    private _curShootTime = 0;
    private _bulletType: number;

    onEnable() {
        const collider = this.getComponent(BoxCollider);
        collider.on("onTriggerEnter", this._onTriggerEnter, this);
        // input.on(Input.EventType.TOUCH_START, this._touchStart, this);
        // input.on(Input.EventType.TOUCH_MOVE, this._touchMove, this);
        // input.on(Input.EventType.TOUCH_END, this._touchEnd, this);
    }
    onDisEnable() {
        const collider = this.getComponent(BoxCollider);
        collider.off("onTriggerEnter", this._onTriggerEnter, this);
        // input.off(Input.EventType.TOUCH_START, this._touchStart, this);
        // input.off(Input.EventType.TOUCH_MOVE, this._touchMove, this);
        // input.off(Input.EventType.TOUCH_END, this._touchEnd, this);
    }
    start() {
    }

    update(deltaTime: number) {
        if (this.curHP == 0) {
            this._isDead = true;
            this.explode.active = true;
            return;
        }
        //玩家子弹发射部分
        this._curShootTime += deltaTime;
        if (this._isShooting && this._curShootTime > this.shootTime) {
            this._curShootTime = 0;
            if (this._bulletType == constant.BulletType.BULLET_H) {
                this.audioManager.play("bullet1");
                this.createPlayerBulletH();
            }
            else if (this._bulletType == constant.BulletType.BULLET_S) {
                this.audioManager.play("bullet1");
                this.createPlayerBulletS();
            }
            else {
                this.audioManager.play("bullet1");
                this.createPlayerBulletM();
            }
        }
    }
    private createPlayerBulletM() {
        // const bullet = instantiate(this.bullet01);//实例化子弹
        // bullet.setParent(this.bulletRoot);//设置子弹父节点
        const bullet = PoolManager.instance().getNode(this.bullet01, this.bulletRoot)
        bullet.setPosition(this.node.position.x, this.node.position.y, this.node.position.z - 2);//默认应该时父节点位置
        const bulletComp = bullet.getComponent(Bullet);//获取子弹节点得脚本组件
        bulletComp.show(this.bulletSpeed, false, constant.Direction.MID);//传递实例化子弹所需要的参数
    }
    private createPlayerBulletH() {
        //left
        // const bulletL = instantiate(this.bullet03);//实例化子弹
        // bulletL.setParent(this.bulletRoot);//设置子弹父节点
        const bulletL = PoolManager.instance().getNode(this.bullet03, this.bulletRoot)
        bulletL.setPosition(this.node.position.x - 2, this.node.position.y, this.node.position.z - 2);//默认应该时父节点位置
        const bulletCompL = bulletL.getComponent(Bullet);//获取子弹节点得脚本组件
        bulletCompL.show(this.bulletSpeed, false, constant.Direction.MID);//传递实例化子弹所需要的参数
        //right
        // const bulletR = instantiate(this.bullet03);//实例化子弹
        // bulletR.setParent(this.bulletRoot);//设置子弹父节点
        const bulletR = PoolManager.instance().getNode(this.bullet03, this.bulletRoot)
        bulletR.setPosition(this.node.position.x + 2, this.node.position.y, this.node.position.z - 2);//默认应该时父节点位置
        const bulletCompR = bulletR.getComponent(Bullet);//获取子弹节点得脚本组件
        bulletCompR.show(this.bulletSpeed, false, constant.Direction.MID);//传递实例化子弹所需要的参数
    }
    private createPlayerBulletS() {
        //Left
        // const bulletL = instantiate(this.bullet05);//实例化子弹
        // bulletL.setParent(this.bulletRoot);//设置子弹父节点
        const bulletL = PoolManager.instance().getNode(this.bullet05, this.bulletRoot)
        bulletL.setPosition(this.node.position.x - 8, this.node.position.y, this.node.position.z - 2);//默认应该时父节点位置
        const bulletCompL = bulletL.getComponent(Bullet);//获取子弹节点得脚本组件
        bulletCompL.show(this.bulletSpeed, false, constant.Direction.LEFT);//传递实例化子弹所需要的参数
        //mid
        // const bulletM = instantiate(this.bullet05);//实例化子弹
        // bulletM.setParent(this.bulletRoot);//设置子弹父节点
        const bulletM = PoolManager.instance().getNode(this.bullet05, this.bulletRoot)
        bulletM.setPosition(this.node.position.x, this.node.position.y, this.node.position.z - 2);//默认应该时父节点位置
        const bulletCompM = bulletM.getComponent(Bullet);//获取子弹节点得脚本组件
        bulletCompM.show(this.bulletSpeed, false, constant.Direction.MID);//传递实例化子弹所需要的参数
        //right
        // const bulletR = instantiate(this.bullet05);//实例化子弹
        // bulletR.setParent(this.bulletRoot);//设置子弹父节点
        const bulletR = PoolManager.instance().getNode(this.bullet05, this.bulletRoot)
        bulletR.setPosition(this.node.position.x + 8, this.node.position.y, this.node.position.z - 2);//默认应该时父节点位置
        const bulletCompR = bulletR.getComponent(Bullet);//获取子弹节点得脚本组件
        bulletCompR.show(this.bulletSpeed, false, constant.Direction.RIGHT);//传递实例化子弹所需要的参数
    }
    //改变子弹发射装置
    public changeBulletType(bulletTpye: number) {
        this._bulletType = bulletTpye;
    }
    // private _touchMove(event: EventTouch) {
    //     const delta = event.getDelta();
    //     let pos = this.node.position;
    //     this.node.setPosition(pos.x + 0.01 * this.speed * delta.x, pos.y, pos.z - 0.01 * this.speed * delta.y);
    // }
    // private _touchStart(event: EventTouch) {
    //     this._isShooting = true;
    // }
    // private _touchEnd(event: EventTouch) {
    //     this._isShooting = false;
    // }
    //响应触发事件
    private _onTriggerEnter(event: ITriggerEvent) {
        const group = event.otherCollider.getGroup();
        const name = event.otherCollider.name;
        //console.log(name);
        if (group == constant.CollisionType.ENEMY || group == constant.CollisionType.ENEMY_BULLET) {
            this.curHP--;
            this.Hp.setScale(this.curHP / this.maxHP, 1, 1);
            //console.log("-HP");
        }
        else if (group == constant.CollisionType.BOSSMISSILE) {
            this.curHP -= 10;
            this.Hp.setScale(this.curHP / this.maxHP, 1, 1);
        }
        else if (group == constant.CollisionType.BULLET_PROP) {
            if (name == "bulletH<BoxCollider>") {
                this._bulletType = constant.BulletType.BULLET_H;
            }
            else if (name == "bulletS<BoxCollider>") {
                this._bulletType = constant.BulletType.BULLET_S;
            }
            else {
                this._bulletType = constant.BulletType.BULLET_M;
            }
        }
    }
    public init() {
        this.curHP = this.maxHP;
        this.Hp.setScale(1, 1, 1);
        this._isDead = false;
        this._curShootTime = 0;
        this._bulletType = constant.BulletType.BULLET_M;
        this.explode.active = false;
        //this.bloodFace.setScale(1, 1, 1);
    }
}

